package com.wiringweb.resolution;

import com.wiringweb.servlet.WiringRequestProcessor;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Dmitry
 */
public class PlainTextResolution implements Resolution {

    private int status = 200;
    private final String message;
    private final static String contentType = "text/plain";

    public PlainTextResolution(int status, String message) {
        this.status = status;
        this.message = message;
    }

    public PlainTextResolution(String message) {
        this.message = message;
    }

    @Override
    public void process(HttpServletRequest request, HttpServletResponse response, WiringRequestProcessor requestProcessor, ServletConfig servletConfig) throws UnsupportedEncodingException, IOException {
        response.setStatus(status);
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        byte[] bytes = message.getBytes("UTF-8");
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
    }
}
